/**
 * WebApp工具类
 * @create 16/2/5.
 * @auth chenw
 * @email javacspring@gmail.com
 */

(function (os) {
    var _date = new Date(), _day = ['日', '一', '二', '三', '四', '五', '六'];

    function _getDate() {
        var _y = _date.getYear() + 1900,
            _m = _date.getMonth() + 1 < 10 ? '0' + (_date.getMonth() + 1) : _date.getMonth() + 1,
            _d = _date.getDate() < 10 ? '0' + _date.getDate() : _date.getDate();
        return [_y, _m, _d].join('-');
    }

    function _getTime() {
        var _h = _date.getHours(), _min = _date.getMinutes(), _s = _date.getSeconds();
        return [_h, _min, _s].join(':');
    }

    function _getDateTime() {
        return _getDate() + " " + _getTime();
    }

    function _getDay() {
        return ['星期', _day[_date.getDate()]].join('');
    }

    function _fullTime() {
        return _getDateTime() + " " + _getDay();
    }

    os.util = {
        getDate: _getDate,
        getTime: _getTime,
        getDateTime: _getDateTime,
        getDay: _getDay,
        fullTime: _fullTime
    };
    window.Oceansoft = os;
})(window.Oceansoft || {});
!function (os) {
    var _local, _root = "oceansoft.cache";
    if (window.localStorage) {
        _local = localStorage;
    } else {
        console.error("当前浏览器不支持本地缓存");
    }
    function _init(profile) {
        if (profile) {
            _root = [_root, profile].join('.');
            return this;
        } else {
            console.error("arguments profile is required");
        }
    }

    function _key(key) {
        if (!key && trim(key)) {
            console.error("key and value is required");
        }
        return [_root, key].join('.');
    }

    function _save(key, value, expire) {
        if (!value || value.indexOf('`')) {
            console.error("value not allow contains character `");
            return;
        }
        if (!expire || isNaN(expire)) {
            expire = -1;
        } else {
            expire = new Date().getTime() + expire * 1000;
        }


        if (value instanceof Object) {
            _local.setItem(_key(key), ['obj', expire, JSON.stringify(value)].join('`'));
        } else if (Array.isArray(value)) {
            _local.setItem(_key(key), ['arr', expire, value.join()].join('`'));
        } else {
            _local.setItem(_key(key), ['str', expire, value].join('`'));
        }
        return true;
    }

    function _get(key) {
        var _cache_obj = _local.getItem(_key(key)).split('`'), _type = _cache_obj[0], _expire = _cache_obj[1], _data = _cache_obj[2];
        if (_expire > 0 && _expire < new Date().getTime()) {
            _remove(_key(key));
            return null;
        }
        switch (_type) {
            case 'str':
            case 'arr':
                return _data;
            case 'obj':
                return JSON.parse(_data);
            default:
                return "";
        }
    }

    function _remove(key) {
        if (!key || !value) {
            console.error("key is empty");
        }
        _local.removeItem(_key(key));
    }

    os.cache = {
        init: _init,
        save: _save,
        get: _get,
        del: _remove
    };
    window.Oceansoft = os;
}(window.Oceansoft || {});
